package com.sojson.util.file.system.impl;

import java.io.File;
import java.io.InputStream;

import com.sojson.constant.IpConfig;
import com.sojson.util.StringUtil;
import com.sojson.util.file.FileUtil;
import com.sojson.util.file.bean.UploadBean;
import com.sojson.util.file.system.FileSystemInterface;
import com.sojson.util.id.IdUtil;

/**
 * 本地文件系统工具类
 *
 * @author liu
 * @Date 2020-06-15 14:45:43
 * @Description
 *
 */
public class FileUtilImpl implements FileSystemInterface {

    private static FileUtilImpl fileUtilsImpl;
    private String bucketName = FileUtil.THIS_LOCALITY_DIRECTORY_BUCKET_NAME_BASE + "/" + BUCKET_NAME;

    private FileUtilImpl() {}

    /**
     * 文件上传
     *
     * @param filePath  文件位置
     * @param dir       上传目录
     * @return
     * @throws Exception
     */
    @Override
    public UploadBean upload(String filePath, String dir) throws Exception {
        StringBuilder targetPathBuilder = mkdir(bucketName, dir);

        String fileName = IdUtil.uuid() + FileUtil.getFileSuffix(filePath);
        FileUtil.write(filePath, targetPathBuilder.append("/").append(fileName).toString());

        return getResult(bucketName, dir, fileName);
    }

    /**
     * 文件上传
     *
     * @param fileStream    文件流
     * @param dir           上传目录
     * @param fileSuffix    文件后缀
     * @return
     * @throws Exception
     */
    @Override
    public UploadBean upload(InputStream fileStream, String dir, String fileSuffix) throws Exception {
        StringBuilder targetPathBuilder = mkdir(bucketName, dir);

        String fileName = IdUtil.uuid() + fileSuffix;
        FileUtil.write(fileStream, targetPathBuilder.append("/").append(fileName).toString());

        return getResult(bucketName, dir, fileName);
    }

    /**
     * 文件上传
     *
     * @param fileBytes     文件字节流
     * @param dir           上传目录
     * @param fileSuffix    文件后缀
     * @return
     * @throws Exception
     */
    @Override
    public UploadBean upload(byte[] fileBytes, String dir, String fileSuffix) throws Exception {
        StringBuilder targetPathBuilder = mkdir(bucketName, dir);

        String fileName = IdUtil.uuid() + fileSuffix;
        FileUtil.write(fileBytes, targetPathBuilder.append("/").append(fileName).toString());

        return getResult(bucketName, dir, fileName);
    }

    /**
     * 创建目录
     * 
     * @param bucketName    桶名称
     * @param dir           目录
     * @return
     */
    private StringBuilder mkdir(String bucketName, String dir) {
        StringBuilder targetPathBuilder = new StringBuilder(bucketName).append("/").append(dir);
        File bucketNameFile = new File(targetPathBuilder.toString());
        if (!bucketNameFile.exists()) {
            bucketNameFile.mkdirs();
        }
        return targetPathBuilder;
    }

    /**
     * 获取上传结果
     * 
     * @param bucketName    桶名称
     * @param dir           目录
     * @param fileName      文件名
     * @return
     */
    private UploadBean getResult(String bucketName, String dir, String fileName) {
        UploadBean upload = new UploadBean();
        upload.setObjectName(new StringBuilder(dir).append("/").append(fileName).toString());
        return upload;
    }

    /**
     * 下载文件
     *
     * @param objectName    唯一标识
     * @param dir           文件路径
     * @param fileName      文件名
     * @return
     * @throws Exception
     */
    @Override
    public boolean download(String objectName, String dir, String fileName) throws Exception {
        File bucketNameFile = new File(dir);
        if (!bucketNameFile.exists()) {
            bucketNameFile.mkdirs();
        }
        FileUtil.write(new StringBuilder(bucketName).append("/").append(objectName).toString(),
            new StringBuilder(FileUtil.THIS_LOCALITY_DIRECTORY_BUCKET_NAME_BASE).append("/").append(dir).append("/")
                .append(fileName).toString());

        return true;
    }

    /**
     * 下载文件
     *
     * @param objectName    唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public InputStream downloadGetStream(String objectName) throws Exception {
        return FileUtil.readInputStream(new StringBuilder(bucketName).append("/").append(objectName).toString());
    }

    /**
     * 下载文件
     *
     * @param objectName    唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public byte[] downloadGetBytes(String objectName) throws Exception {
        return FileUtil.readByes(new StringBuilder(bucketName).append("/").append(objectName).toString());
    }

    /**
     * 删除文件
     *
     * @param objectName    唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public boolean remove(String objectName) throws Exception {
        FileUtil.delete(new StringBuilder(bucketName).append("/").append(objectName).toString());

        return true;
    }

    /**
     * 获取文件路径
     *
     * @param objectName    唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public String getUrl(String objectName) throws Exception {
        return IpConfig.getUrl() + objectName;
    }

    /**
     * 获取文件路径(临时路径)
     *
     * @param objectName    唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public String getUrlTemporary(String key) {
        return null;
    }

    /**
     * 获取文件路径(临时路径)
     *
     * @param objectName    唯一标识
     * @param timeout       超时时间(单位: 秒)
     * @return
     * @throws Exception
     */
    @Override
    public String getUrlTemporary(String objectName, int timeout) throws Exception {
        return null;
    }

    /**
     * 判断一个对象是否是空(空对象)
     *
     * @param obj
     * @return
     */
    public static boolean isBlankObject(Object obj) {
        return StringUtil.isBlankObject(obj);
    }

    public static FileUtilImpl getInstances() {
        if (isBlankObject(fileUtilsImpl)) {
            fileUtilsImpl = new FileUtilImpl();
        }
        return fileUtilsImpl;
    }

}